//
//  UpdateTipsView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/7.
//

#import "UpdateTipsView.h"

@interface UpdateTipsView ()

@property (nonatomic, strong) UIView *contentView;

@end

@implementation UpdateTipsView

- (void)initViews {
    self.backgroundColor = kRGB_ALPHA(0x000000, 0.3);
    [self addSubview:self.contentView];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"updateTip"]];
    [self.contentView addSubview:imageV];
    
    UIButton *updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    updateBtn.layer.cornerRadius = 20;
    updateBtn.layer.borderColor = kBgColor.CGColor;
    updateBtn.layer.borderWidth = 1;
    [updateBtn setTitle:@"直接同步更新" forState:UIControlStateNormal];
    [updateBtn setTitleColor:kBgColor forState:UIControlStateNormal];
    [self.contentView addSubview:updateBtn];
    @weakify(self)
    [[updateBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        if (self.updateShopBlock) {
            self.updateShopBlock();
        }
    }];
    
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkBtn setTitle:@"查看更新内容" forState:UIControlStateNormal];
    [checkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    checkBtn.layer.cornerRadius = 20;
    checkBtn.layer.masksToBounds = YES;
    checkBtn.backgroundColor = kBgColor;
    [self.contentView addSubview:checkBtn];
    [[checkBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        if (self.checkContentBlock) {
            self.checkContentBlock();
        }
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFontMediium(18);
    titleLabel.textColor = kRGB(0x333333);
    titleLabel.text = @"商家信息有更新哦~";
    [titleLabel sizeToFit];
    [self.contentView addSubview:titleLabel];
    
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(230, 40));
        make.bottom.mas_equalTo(-24);
        make.centerX.mas_equalTo(0);
    }];
    [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(230, 40));
        make.bottom.mas_equalTo(updateBtn.mas_top).offset(-12);
        make.centerX.mas_equalTo(0);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(checkBtn.mas_top).offset(-24);
        make.centerX.mas_equalTo(0);
    }];
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake((kWidthForScreen-33)/2, (kHeightForScreen-280)/2, 330, 280)];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}

- (void)show {
    self.tag = 8888;
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    [self addAnimate];
}

- (void)hide {
    [self removeFromSuperview];
}

- (void)addAnimate {
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.contentView.layer addAnimation:animation forKey:nil];
}

@end
